//
//  Second.m
//  A段项目
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "Second.h"
#import "NetWorkingHandler.h"
#import "Model.h"
#import "FindOfTableViewCell.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>
#import "LbtWebView.h"

@interface Second ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain) NSMutableArray *arrModel;
@property (nonatomic, retain) UITableView *tableView;


@end

@implementation Second

- (void)dealloc {
    
    [super dealloc];
    
    [_tableView release];
    [_arrModel release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createTableView];
    
    [self dataArrayModel];
    
    
    [self handleData];
    
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self handleData];
    }];
    
}

- (void)addHud {
    
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        
        if (self.arrModel != 0) {
            
            [MBProgressHUD hideHUDForView:self.tableView animated:YES];
            
        }
        
    });

    
    
}

- (NSArray *)dataArrayModel {
    
    if (self.arrModel == nil) {
        
        [self addHud];

        
        self.arrModel = [NSMutableArray array];
        
    }
    
    return self.arrModel;
}

- (void)handleData {
    
    NSString *string = @"http://mapp.tiankong.com/articleSys/list?pageSize=30";
    
    [NetWorkingHandler netWorkingWithUrl:string complete:^(id result) {
        
        NSDictionary *dic = [result objectForKey:@"data"];
        
        NSArray *arr = [dic objectForKey:@"page"];
        
        for (NSDictionary *dic in arr) {
            
            Model *model = [[Model alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrModel addObject:model];
            
           
        }
        
        [self.tableView reloadData];
        
        [self.tableView.mj_header endRefreshing];
        
    }];
    
}


- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:0];
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:self.tableView];
    
   
    
    self.tableView.separatorStyle = 0;
    
    
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[FindOfTableViewCell class] forCellReuseIdentifier:@"pool1"];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.arrModel.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FindOfTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"pool1"];
    
    Model *model = [self.arrModel objectAtIndex:indexPath.row];
    
    cell.separatorInset = UIEdgeInsetsMake(100, 100, 100, 100);
    
   
    
    [cell passedModel:model];
   
    cell.selectionStyle = 0;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Model *modle = [self.arrModel objectAtIndex:indexPath.row];
    
    return [FindOfTableViewCell heightForCell:modle] + 30;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LbtWebView *web = [[LbtWebView alloc] init];
    
    Model *model = [self.arrModel objectAtIndex:indexPath.row];
    
    web.link = [NSString stringWithFormat:@"http://mapp.tiankong.com/articleSys/show?id=%ld",(long)model.id];
    
    [self.navigationController pushViewController:web animated:YES];
    
    [web release];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
